/**
  ******************************************************************************
  * @file    stringget.c
  * @author  yan
  * @version 
  * @date    2016-12-21
  * @brief   
  ******************************************************************************
	*/

/*------------include----------------*/
#include <rtthread.h>
#include "stringget.h"
#include  "stdio.h"
#include  "ctype.h"
#include  "stdlib.h"
#include  "string.h"    


#define SPEEDARG  "speed" 
#define AXISARG  "axis"
#define PULSEARG  "pulse"

/**
  * @brief 
  * @param  
  * @retval 
  */
rt_uint8_t stringjudge(char ar[],char ar_flag[])
{

	if(strstr(ar,ar_flag)!=NULL)
	{
		memset(ar,1,strlen(ar_flag));
//		printf("STR_flag success\n");
		return 0;
	}
	else
	{
//		printf("STR_flag fail\n");
		return 1;
	}
}


/**
  * @brief 
  * @param  
  * @retval 
  */
int regular(char ar_flag[],char ar[])
{

	char *STR_ADD;
	int flag_shift;
	STR_ADD=strstr(ar,ar_flag);
	flag_shift=strlen(ar_flag);
	int number_sbit=0;
	char *numberstr;
  
//  rt_kprintf("\r\nregular is:%s flag is %s\r\n",ar,ar_flag);
	while(isalnum(STR_ADD[flag_shift+number_sbit])==32)
	{
		number_sbit++;
	}
  
	numberstr=malloc(sizeof(char)*(number_sbit+1));
  if(numberstr==NULL){
    rt_kprintf("cant get mem\r\n");
    return 0;
  }
//  rt_kprintf("STR_ADD is :%s number : %d \r\n",STR_ADD,number_sbit);
	memcpy(numberstr,&STR_ADD[flag_shift],number_sbit);
	numberstr[number_sbit]='\0';
//  rt_kprintf("find sub string %s\r\n",numberstr);
  
	int number = atoi(numberstr);
	free(numberstr);
  
	return number;
}
/**
  * @brief 
  * @param  
  * @retval 
  */
int getspeed(rt_uint8_t * str)
{
  return regular(SPEEDARG,(char *)str);
}
/**
  * @brief 
  * @param  
  * @retval 
  */
int getaxis(rt_uint8_t * str)
{
  return regular(AXISARG,(char *)str);
}

/**
  * @brief 
  * @param  
  * @retval 
  */
int getpulse(rt_uint8_t * str)
{
  return regular(PULSEARG,(char *)str);
}

#include <finsh.h>

rt_uint16_t  RegTest(int argc, char** argv)
{

//  int result;
//  char buf[] = "revcontinuous: speed:4000 axis:15";
  
  return isalnum('9');
  
}
FINSH_FUNCTION_EXPORT(RegTest,regextest);

